package com.cmwa.solution.business.demand.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.demand.entity.PrdDemandVo;
import com.cmwa.solution.business.demand.service.PrdDemandService;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.sys.attach.service.AttachService;
import com.cmwa.solution.sys.msg.util.StringUtil;
import com.cmwa.solution.utils.cached.DictionaryCached;
import com.google.common.base.Joiner;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/business/product/demand")
public class PrdDemandController extends BaseController {

	private final static String JSP_PATH = "business/demand/";
	
	@Resource
	private PrdDemandService prdDemandService;
	
	@Resource
	private AttachService attachService;

	@RequestMapping("/overview")
	public String overview() {
		return JSP_PATH+"demandOverview";
	}
	
	@RequestMapping("/demandHome")
	public String demandHome(@RequestParam(value="oper") String oper,
			@RequestParam(value="url") String url,ModelMap model) {
		model.addAttribute("oper", oper);
		model.addAttribute("url", url);
		return JSP_PATH+"demandHome";
	}
	
	@RequestMapping("/addDemand")
	public String addDemand(ModelMap model) {
		List<?> dTypes = DictionaryCached.getDictionaryByType("PRD_DEMAND_TYPE", "PRD_DEMAND_TYPE", "PRD_DEMAND_TYPE");
		List<?> mTypes = DictionaryCached.getDictionaryByType("D_MATERIAL_TYPE", "D_MATERIAL_TYPE", "D_MATERIAL_TYPE");
		model.addAttribute("dTypes", dTypes);
		model.addAttribute("mTypes", mTypes);
		return JSP_PATH+"addDemand";
	}
	
	
	@RequestMapping("/demandList")
	public String demandList(ModelMap model) {
		List<?> statusList = DictionaryCached.getDictionaryByType("PRD_DEMAND_STATUS", "PRD_DEMAND_STATUS", "PRD_DEMAND_STATUS");
		List<?> typeList = DictionaryCached.getDictionaryByType("PRD_DEMAND_TYPE", "PRD_DEMAND_TYPE", "PRD_DEMAND_TYPE");
		//List<?> matTypeList = DictionaryCached.getDictionaryByType("D_MATERIAL_TYPE", "D_MATERIAL_TYPE", "D_MATERIAL_TYPE");

		model.addAttribute("statusList", statusList);
		model.addAttribute("typeList", typeList);
		//model.addAttribute("matTypeList", matTypeList);
		return JSP_PATH+"demandList";
	}
	
	/**
	 * 分页查询
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/prdDemandListPage")
	public Page<PrdDemandVo> prdDemandListPage(SearchParam param) {
		Page<PrdDemandVo> list = prdDemandService.prdDemandListPage(param);
		return list;
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/saveDemand")
	public String saveDemand(PrdDemandVo demandVo) {
		prdDemandService.saveDemand(demandVo, demandVo.getAttIds());
		return redirectSuccess();
	}
	
	@RequestMapping("/demandDetail")
	public String demandDetail(@RequestParam(value="demandId") String demandId,ModelMap model) {
		PrdDemandVo demandVo = prdDemandService.demandDetail(demandId);
		model.put("demandVo", demandVo);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(demandId, "DEMAND_INFO");
		model.addAttribute("attachVos", attachVos);
		return JSP_PATH+"demandDetail";
	}
	
	
	@RequestMapping("/updateView")
	public String updateView(@RequestParam(value="demandId") String demandId,ModelMap model) {
		PrdDemandVo demandVo = prdDemandService.demandDetail(demandId);
		List<?> dTypes = DictionaryCached.getDictionaryByType("PRD_DEMAND_TYPE", "PRD_DEMAND_TYPE", "PRD_DEMAND_TYPE");
		List<?> mTypes = DictionaryCached.getDictionaryByType("D_MATERIAL_TYPE", "D_MATERIAL_TYPE", "D_MATERIAL_TYPE");
		model.addAttribute("dTypes", dTypes);
		model.addAttribute("mTypes", mTypes);
		model.put("demandVo", demandVo);
		String archive2Json = "";
		//加载附件信息
		List<AttachVo> attaches = attachService.queryAttachByRelIdAndType(demandId, "DEMAND_INFO");
		//如果附件信息不为空，则加载附件信息
		if(null != attaches) {
			archive2Json = StringUtil.changeSpecialCharacter(JSONArray.fromObject(attaches).toString());
		}
		model.addAttribute("attachList", archive2Json);
		return JSP_PATH+"updateDemand";
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/updateDemand")
	public String updateDemand(PrdDemandVo demandVo) {
		prdDemandService.updateDemand(demandVo, demandVo.getAttIds());
		return redirectSuccess();
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result<PrdDemandVo> delete(@RequestParam("demandId")String demandId) {
		Result<PrdDemandVo> result = new Result<PrdDemandVo>();
		result.setSuccess(true);
		result.setMessage("删除成功");
		try {
			prdDemandService.deleteDemandById(demandId);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("删除失败");
		}
		return result;
	}
	
	/**
	 * 打包下载附件
	 * @param id
	 * @throws IOException 
	 */
	@RequestMapping("/download")
	public void download(@RequestParam("demandId")String demandId, HttpServletResponse response, HttpServletRequest request) throws IOException {
		/*PrdDemandVo demandVo = new PrdDemandVo();
		demandVo.setDemandId(demandId);*/
		PrdDemandVo demandVo = prdDemandService.demandDetail(demandId);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(demandId, "DEMAND_INFO");
		String fileName = "产品创设需求";
		if (demandVo != null ) {
			fileName = demandVo.getTitle();
		}
		
		if (demandVo != null && CollectionUtils.isNotEmpty(attachVos)) {
			List<String> attIds = new ArrayList<>();
			for (AttachVo attach : attachVos) {
				attIds.add(attach.getAttId());
			}
			attachService.downloadAttachZip(Joiner.on(",").join(attIds), fileName, request, response);
		} else {
			attachService.downloadAttachZip("", fileName, request, response);
		}
	}
}
